/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package testapp;

import comunicacao.ReceiveMessageInterface;
import entities.Lugar;
import entities.Teatro;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.InetAddress;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.ArrayList;

/**
 *
 * @author Leonardo Lino
 */
public class GestorComunicacao extends java.rmi.server.UnicastRemoteObject
        implements ReceiveMessageInterface {

    int thisPort;
    String thisAddress = "127.0.0.1";
    Registry registry; // rmi registry for lookup the remote objects.
    AppDatabaseComunicator appDB;

    public GestorComunicacao() throws RemoteException {
        String everything = "";
        String dbPort = "";
        String dbAddress = "";
        String port = "0";

        // Open config file
        try {
            FileReader fr = new FileReader("config.txt");
            BufferedReader br = new BufferedReader(fr);

            StringBuilder sb = new StringBuilder();
            String line = br.readLine();

            while (line != null) {
                sb.append(line);
                sb.append('\n');
                line = br.readLine();
            }

            everything = sb.toString();
        } catch (IOException e) {
        }

        String args[] = everything.split("\\s+");

        // Load config file
        for (int i = 0; i < args.length; i++) {
            String[] lineChunks = args[i].split(":");
            if (lineChunks[0].equals("Port")) {
                port = lineChunks[1]; // (registry port)
            } else if (lineChunks[0].equals("DBServPort")) {
                dbPort = lineChunks[1];
            } else if (lineChunks[0].equals("DBServAddress")) {
                dbAddress = lineChunks[1];
            }
        }

        startRecebe(port);
        appDB = new AppDatabaseComunicator(dbAddress, dbPort);
    }

    public void startRecebe(String serverPort) throws RemoteException {

        try {
            // get the address of this host.
            thisAddress = (InetAddress.getLocalHost()).toString();
        } catch (Exception e) {
            throw new RemoteException("can't get inet address.");
        }

        thisPort = Integer.parseInt(serverPort);
        System.out.println("this address=" + thisAddress + ",port=" + thisPort);

        try {
            // create the registry and bind the name and object.
            registry = LocateRegistry.createRegistry(thisPort);
            registry.rebind("AppServer", this);
            System.out.println("AQUUUUUUUUUUIIIIIIIIIIIIIIIIIIIOK");
        } catch (RemoteException e) {
            System.out.println("AQUUUUUUUUUUIIIIIIIIIIIIIIIIIIIFAIL");
            throw e;
        }

    }

    @Override
    public ArrayList<String> listTheaters() throws RemoteException {
        System.out.println("pedido lista teatro");
        return appDB.listTheaters();
    }

    @Override
    public Teatro getTheater(String theaterName) throws RemoteException {
        return appDB.getTheater(theaterName);
    }

    @Override
    public boolean reserveSeat(String theaterName, int lugarID, int userID) throws RemoteException {
        return appDB.reserveSeat(theaterName, lugarID, userID);
    }

    @Override
    public boolean purchaseSeat(String theaterName, int lugarID, int userID) throws RemoteException {
        return appDB.purchaseSeat(theaterName, lugarID, userID);
    }

    @Override
    public boolean falha() throws RemoteException {
        return appDB.falha();
    }

    @Override
    public boolean restart() throws RemoteException {
        return appDB.restart();
    }

}
